<template>
  <div>
    <el-dialog
      v-loading="loading"
      :visible.sync="dialog"
      :close-on-click-modal="false"
      title="Item详情"
      width="85%"
      @close="cancel"
      @opened="initPaneData"
    >
      <div class="app-container">
        <el-button v-if="parent.readOnly !== true && itemStatus === 2" style="float:right;margin-left:0px;" size="mini" type="primary" icon="el-icon-monitor" @click="doValidate">校验</el-button>
        <el-button v-if="parent.readOnly !== true && itemStatus === 3" style="float:right;margin-left:0px;" size="mini" type="primary" icon="el-icon-monitor" @click="doReload">刷新</el-button>
        <div style="float:left;">
          <record :oid="id" />
        </div>
        <div style="float:left;">
          <validateMsg ref="validateMsg" :item="itemInfo" :type="2" />
        </div>
        <div style="padding-top:40px">
          <el-tabs type="border-card">
            <el-tab-pane label="inputSheet" name="inputSheet">
              <inputSheetForm ref="inputSheetForm" :iparent="iparent" />
            </el-tab-pane>
            <el-tab-pane label="在售商品" name="onSale">
              <onSaleForm ref="onSaleForm" />
            </el-tab-pane>
            <el-tab-pane label="vendor_terms" name="vendorTerms">
              <vendorTermsForm ref="vendorTermsForm" />
            </el-tab-pane>
            <el-tab-pane label="PLU申请表" name="plu">
              <pluForm ref="pluForm" :iparent="iparent" />
            </el-tab-pane>
            <el-tab-pane label="附件表" name="mps">
              <mpsFileForm ref="mpsFileForm" />
            </el-tab-pane>
            <!-- <el-tab-pane label="附件退货证明" name="returns">附件退货证明</el-tab-pane> -->
            <el-tab-pane label="Price OB" name="price">
              <priceForm ref="priceForm" :iparent="iparent" />
            </el-tab-pane>
            <el-tab-pane label="IGRO Form" name="igroForm">
              <igroForm ref="igroForm" />
            </el-tab-pane>
            <el-tab-pane label="Ftag Form" name="ftagForm">
              <ftagForm ref="ftagForm" />
            </el-tab-pane>
            <el-tab-pane label="CPK&Unit" name="cpkUnit">
              <cpkAndUnitForm ref="cpkAndUnitForm" />
            </el-tab-pane>
            <el-tab-pane label="PB竞品商品信息" name="pb">
              <pbItemForm ref="pbItemForm" />
            </el-tab-pane>
            <el-tab-pane label="PI_激活商品" name="active">
              <activeForm ref="activeForm" :iparent="iparent" />
            </el-tab-pane>
            <el-tab-pane label="PI_解交叉" name="ixiu">
              <ixiuForm ref="ixiuForm" :iparent="iparent" />
            </el-tab-pane>
            <el-tab-pane label="PI_Trait变更" name="trait">
              <traitForm ref="traitForm" :iparent="iparent" />
            </el-tab-pane>
          </el-tabs>
        </div>
        <div v-if="parent.readOnly !== true" style="float:right;">
          <el-button v-if="itemStatus === 2 || itemStatus === 4 || itemStatus === -4 || itemStatus === -10 || itemStatus === -6 || itemStatus === -11 || itemStatus === 17" type="primary" size="mini" @click="doSave">保存</el-button>
          <el-button v-if="itemStatus === 1" type="success" size="mini" @click="batchAgreeCoiFlag">同意</el-button>
          <el-button v-if="itemStatus === 1" type="danger" size="mini" @click="doReject">驳回</el-button>
        </div>
      </div>
    </el-dialog>
    <choice ref="choice" :parent="parent_info" />
  </div>
</template>

<script>
import choice from '@/views/new-item/choice/choice'
import traitForm from './sheet/trait'
import ixiuForm from './sheet/ixiu'
import activeForm from './sheet/active'
import inputSheetForm from './sheet/inputSheet'
import onSaleForm from './sheet/onSale'
import vendorTermsForm from './sheet/vendorTerms'
import pluForm from './sheet/plu'
import priceForm from './sheet/price'
import igroForm from './sheet/igro'
import ftagForm from './sheet/ftag'
import cpkAndUnitForm from './sheet/cpkAndUnit'
import pbItemForm from './sheet/pbItem'
import mpsFileForm from './sheet/mpsFile'
import { batchFirstAuditAgree, batchFirstAuditReject, saveItemInfo, batchValidate, get } from '@/api/new-item'
import { Message } from 'element-ui'
import validateMsg from '@/views/part/validateMsg'
import record from '@/views/part/record'
import { selectApproval, approvalTask } from '@/api/reason/dataO2o'

export default {
  components: { choice, inputSheetForm, onSaleForm, vendorTermsForm, priceForm, igroForm, ftagForm, cpkAndUnitForm, pbItemForm, mpsFileForm, validateMsg, pluForm, record, activeForm, ixiuForm, traitForm },
  props: {
    parent: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      gridData: [],
      dialog: false,
      itemInfo: {},
      loading: false,
      step: 0,
      id: '',
      itemStatus: 0,
      iparent: this,
      rowsApproval: {},
      parent_info: this
    }
  },
  methods: {
    async  batchAgreeCoiFlag() {
      if (window.coiFlag) {
        const result = await approvalTask()
        console.log(result)
        this.rowsApproval = []
        console.log(this.rowsApproval)
        console.log(this.rowsApproval.length)
        this.url = '/approvalO2oController/selectApproval'
        await selectApproval(this.url, [this.id]).then(res => {
          console.log(res)
          res.result.forEach(item => {
            // eslint-disable-next-line eqeqeq
            if ((item.status == 2 && item.status1 == 1 && item.status2 == 1 && item.status3 == 1) || (item.status == 2 && item.status1 == null && item.status2 == null && item.status3 == null)) {
              this.gridData.forEach(item2 => {
                // eslint-disable-next-line eqeqeq
                if (item2.id == item.itemId) {
                  console.log(item2.id + '---' + item.itemId)
                  this.rowsApproval.push(item2)
                }
              })
            }
          })
        }, error => {
          console.log(error)
          this.loading = false
        })
        console.log(this.rowsApproval)
        console.log(this.rowsApproval.length)
        if (this.rowsApproval.length < 1) {
          this.doAgreeCoi()
        } else {
          this.agreeCoi()
        }
        console.log('window.coiFlag:' + window.coiFlag)
        console.log('window.coiFlag:true' + window.coiFlag)
      } else {
        console.log('window.coiFlag:false' + window.coiFlag)
        this.doAgree()
      }
    },
    agreeCoi() {
      const _this = this.$refs.choice
      _this.dialogTableVisible = true
      _this.gridData = this.gridData
      _this.ids = [this.id]
    },
    // 审核通过
    doAgreeCoi() {
      this.loading = true
      batchFirstAuditAgree([this.id]).then(res => {
        this.loading = false
        this.itemStatus = 2
        this.step = 2
        this.parent.init()
        Message({
          message: '审核完成。',
          type: 'success',
          duration: 3 * 1000
        })
      }, error => {
        console.log(error)
        this.loading = false
      })
    },
    // 审核通过
    doAgree() {
      this.loading = true
      this.$confirm('确认审核通过该item信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        batchFirstAuditAgree([this.id]).then(res => {
          this.loading = false
          this.itemStatus = 2
          this.step = 2
          this.parent.init()
          Message({
            message: '审核完成。',
            type: 'success',
            duration: 3 * 1000
          })
        }, error => {
          console.log(error)
          this.loading = false
        })
      })
    },
    // 审核驳回
    doReject() {
      this.loading = true
      this.$prompt('请输入驳回原因', '驳回Item', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        inputValidator: this.rejectRemarkValidator,
        type: 'warning'
      }).then((rejectRemark) => {
        batchFirstAuditReject([this.id], rejectRemark.value).then(res => {
          this.loading = false
          this.itemStatus = -2
          this.step = 0
          this.parent.init()
          Message({
            message: '审核完成。',
            type: 'success',
            duration: 3 * 1000
          })
        }, error => {
          console.log(error)
          this.loading = false
        })
      })
    },
    // 驳回原因校验
    rejectRemarkValidator(val) {
      if (!val) {
        return '请填写驳回原因'
      } else if (val.length > 500) {
        return '驳回原因长度为1-500'
      }
      return true
    },
    // 保存触发
    doSave() {
      this.$confirm('是否保存该item信息?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.itemInfo.priceOBFormInfo = this.$refs.priceForm.priceOBFormInfo
        const data = { id: this.id, itemInfo: JSON.stringify(this.itemInfo) }
        saveItemInfo(data).then(() => {
          this.parent.init()
          this.itemStatus = 2
          Message({
            message: '保存成功。',
            type: 'success',
            duration: 3 * 1000
          })
          this.loading = false
        }, error => {
          console.log(error)
          this.loading = false
        })
      })
    },
    cancel() {
      this.itemInfo = {}
      this.loading = false
      this.id = ''
      this.itemStatus = 0
      this.parent.toQuery()
    },
    // 校验
    doValidate() {
      this.$confirm('确认校验该item信息?', '校验数据', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        batchValidate({ ids: [this.id] }).then(() => {
          get(this.id).then(res => {
            this.itemStatus = res.itemStatus
            this.itemInfo = JSON.parse(res.itemInfo)
            this.initPaneData()
            this.parent.init()
            this.$alert('校验处理中，请稍后点击刷新按钮进行查询。', '提交成功', {
              confirmButtonText: '确定',
              type: 'success'
            })
            this.loading = false
          }, (error) => {
            console.log(error)
            this.loading = false
          })
        }, (error) => {
          console.log(error)
          this.loading = false
        })
      })
    },
    // 刷新
    doReload() {
      this.loading = true
      get(this.id).then(res => {
        this.itemStatus = res.itemStatus
        this.itemInfo = JSON.parse(res.itemInfo)
        this.initPaneData()
        this.loading = false
      }, (error) => {
        console.log(error)
        this.loading = false
      })
    },
    // 初始化加载
    initPaneData() {
      this.$refs.inputSheetForm.itemInfo = this.itemInfo
      this.$refs.onSaleForm.salesItem = this.itemInfo.salesItem ? this.itemInfo.salesItem : {}
      this.$refs.ftagForm.ftagFormInfo = this.itemInfo.ftagFormInfo ? this.itemInfo.ftagFormInfo : {}
      this.$refs.vendorTermsForm.vendorTermInfo = this.itemInfo.vendorTerms ? this.itemInfo.vendorTermInfo : []
      if (this.itemInfo.pluItemInfo) {
        this.$refs.pluForm.pluItemInfo = this.itemInfo.pluItemInfo
        this.$refs.pluForm.editFlag = true
      } else {
        this.$refs.pluForm.editFlag = false
      }
      if (this.itemInfo.priceOBFormInfo) {
        this.$refs.priceForm.priceOBFormInfo = this.itemInfo.priceOBFormInfo
        this.$refs.priceForm.editFlag = true
      } else {
        this.$refs.priceForm.priceOBFormInfo = []
        this.$refs.priceForm.editFlag = false
      }
      this.$refs.igroForm.igroFormInfo = this.itemInfo.igroFormInfo ? this.itemInfo.igroFormInfo : {}
      this.$refs.cpkAndUnitForm.cpkAndUnitInfo = this.itemInfo.cpkAndUnitInfo ? this.itemInfo.cpkAndUnitInfo : {}
      this.$refs.pbItemForm.pbItemInfo = this.itemInfo.pbItemInfo ? this.itemInfo.pbItemInfo : {}
      this.$refs.mpsFileForm.itemInfo = this.itemInfo ? this.itemInfo : {}
      this.$refs.mpsFileForm.vendorItemInfo = this.itemInfo.vendorItemInfo ? this.itemInfo.vendorItemInfo : {}
    },
    // vsk#变更联动PLU申请表的PLU NBR与price OB表的VSK#
    changePluNbr(vskNbr) {
      if (this.itemInfo.pluItemInfo) {
        this.itemInfo.pluItemInfo.pluNbr = vskNbr
      }
      this.itemInfo.priceOBFormInfo.forEach(element => {
        element.vendorStockId = vskNbr
        this.$set(this.itemInfo.priceOBFormInfo, element)
      })
      this.$refs.priceForm.priceOBFormInfo.forEach(element => {
        element.vendorStockId = vskNbr
        this.$set(this.$refs.priceForm.priceOBFormInfo, element)
      })
    }
  }
}
</script>

